package com.matt.controller.pad;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.DO.ClassListDO;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.ClassesService;
import com.matt.service.PhoneUserService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

;

/**
 * @Describle This Class Is 老师端麦信
 * @Author ZengMin
 * @Date 2019/1/2 11:01
 * @Company Matt
 */
@RestController
@RequestMapping("/pad/contact")
@Api(tags = "通讯录")
public class PadContactController {

    @Autowired
    SsoCallService ssoCallService;

    @Autowired
    MemVerificationCache cache;

    @Autowired
    private PhoneUserService phoneUserService;

    @Autowired
    private ClassesService classesService;

    //老师获取通讯录
    @ApiOperation("根据班级获取所有家长")
    @GetMapping("/parents")
    public StatusResponse getDialogs(@RequestHeader String ticket,
                                     String classId) {

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        String roleCode = PermissionConstant.Role.TEACHER.codeOf();
        if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_CLASSES_FULL})){
            roleCode = PermissionConstant.Role.GARDEN_LEADER.codeOf();
        }else if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_CLASSES_DOCTOR})){
            roleCode = PermissionConstant.Role.DOCTOR.codeOf();
        }

        List<ClassListDO> classesList = classesService.getClassesByStaffId(roleCode, staff.getId(), staff.getAreaId());

        List<ClassListDO> classes = classesList.stream()
                .filter(o->classId==null||o.getId().equals(classId))
                .collect(Collectors.toList());

        //根据老师id 取班级 取所有孩子 取所所有家长
        return StatusResponse.success(phoneUserService.getAllParentsByClasses(staff.getAreaId(), staff.getId(), classes));
    }

    @ApiOperation("根据班级获取所有老师")
    @GetMapping("/teachers")
    public StatusResponse getAreaDialogs(@RequestHeader String ticket,
                                         String classId) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        String roleCode = PermissionConstant.Role.TEACHER.codeOf();
        if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_CLASSES_FULL})){
            roleCode = PermissionConstant.Role.GARDEN_LEADER.codeOf();
        }else if (ssoCallService.checkStaffPermission(ticket, new String[]{PermissionConstant.TEACH_CLASSES_DOCTOR})){
            roleCode = PermissionConstant.Role.DOCTOR.codeOf();
        }

        List<ClassListDO> classesList = classesService.getClassesByStaffId(roleCode, staff.getId(), staff.getAreaId());

        List<ClassListDO> classes = classesList.stream()
                .filter(o->classId==null||o.getId().equals(classId))
                .collect(Collectors.toList());

        //根据老师id 取班级 取所有孩子 取所所有家长
        return StatusResponse.success(phoneUserService.getAllTeachers(ticket, staff.getAreaId(), staff.getId(), classes));
    }


}
